//
//  LLRemarkCell.swift
//  LiLi
//
//  Created by liuzhu on 2019/1/24.
//  Copyright © 2019 liuzhu. All rights reserved.
//

import UIKit

class LLRemarkCell: UITableViewCell {
    
    public var remark:LLRemark?{
        didSet{
            let time = Int((remark?.createTime!)!)
            let dateStr = timeStampToString(timeStamp: time!)
            timeLabel.text = dateStr
            
            let paraph = NSMutableParagraphStyle()
            //将行间距设置为28
            paraph.lineSpacing = 5
            //样式属性集合
            let attributes = [NSAttributedString.Key.font:UIFont.systemFont(ofSize: 13),
                              NSAttributedString.Key.paragraphStyle: paraph]
            desLabel.attributedText = NSAttributedString(string: (remark?.des)!, attributes: attributes)
        }
    }
    
    private func timeStampToString(timeStamp:Int)->String {
        let timeSta:TimeInterval = Double(timeStamp)
        let dfmatter = DateFormatter()
        dfmatter.timeZone = (NSTimeZone.init(abbreviation: "UTC")! as TimeZone)
        dfmatter.dateFormat="yyyy-MM-dd HH:mm:ss"
        let date = NSDate(timeIntervalSince1970: timeSta)
        print(dfmatter.string(from: date as Date))
        return dfmatter.string(from: date as Date)
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        // 必须加入的方法
        fatalError("init(coder:) has not been implemented")
    }

    private func setupUI() {
        self.backgroundColor = LLConst.backgroundGray()
        contentView.addSubview(topView)
        topView.addSubview(timeLabel)
        contentView.addSubview(desLabel)
        contentView.addSubview(deleteView)
        contentView.addSubview(bottomView)
        topView.xmg_AlignInner(type: XMG_AlignType.TopLeft, referView: contentView, size: CGSize(width: UIScreen.main.bounds.size.width, height: 30), offset: CGPoint(x: 0, y: 0))
        desLabel.xmg_AlignVertical(type: XMG_AlignType.BottomLeft, referView: topView, size: nil, offset: CGPoint(x: 5, y: 5))
        deleteView.xmg_AlignVertical(type: XMG_AlignType.BottomLeft, referView: desLabel, size:  CGSize(width: UIScreen.main.bounds.size.width, height: 20), offset: CGPoint(x: -5, y: 0))
        bottomView.xmg_AlignVertical(type: XMG_AlignType.BottomLeft, referView: deleteView, size:  CGSize(width: UIScreen.main.bounds.size.width, height: 10), offset: CGPoint(x: 0, y: 0))
        bottomView.xmg_AlignInner(type: XMG_AlignType.BottomRight, referView: contentView, size: nil, offset: CGPoint(x: 0, y: 0))
    }

    private lazy var topView: UIView = {
        let ve = UIView()
        ve.backgroundColor = LLConst.lightGray()
        return ve
    }()
    
    private lazy var timeLabel:UILabel = {
        let la = UILabel(frame: CGRect(x: 5, y: 0, width: UIScreen.main.bounds.size.width - 65, height: 30))
        la.backgroundColor = UIColor.clear
        la.font = UIFont.systemFont(ofSize: 14)
        la.textColor = UIColor.darkGray
        return la
    }()
    
    private lazy var desLabel:UILabel = {
        let la = UILabel()
        la.backgroundColor = UIColor.clear
        la.preferredMaxLayoutWidth = UIScreen.main.bounds.width - 10
        la.numberOfLines = 0
        la.textColor = UIColor.lightGray
        la.font = UIFont.systemFont(ofSize: 13)
        return la
    }()
    
    
    private lazy var deleteView: UIView = {
        let ve = UIView()
        ve.backgroundColor = UIColor.clear
        ve.addSubview(deleteBtn)
        return ve
    }()
    
    private lazy var bottomView: UIView = {
        let ve = UIView()
        ve.backgroundColor = UIColor.white
        return ve
    }()
    
    public lazy var deleteBtn:UIButton = {
        let bt = UIButton.init(frame: CGRect(x: UIScreen.main.bounds.size.width - 30, y: 0, width: 20, height: 20))
        bt.setBackgroundImage(UIImage(named: "delete"), for: UIControl.State.normal)
        return bt
    }()
}
